package juc;

import java.util.concurrent.atomic.AtomicInteger;

class MyRunnable1 implements Runnable{
    private AtomicInteger num = new AtomicInteger();
    @Override
    public void run() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(getNum());
    }

    public int getNum() {
        return num.getAndIncrement(); // num++ 保证了每次读取值和修改值是原子性的，其他线程不会打断
    }
}

public class MyAutomic {
    public static void main(String[] args) {
        MyRunnable1 myRunnable1 = new MyRunnable1();
        for (int i= 0;i<10;i++){
            new Thread(myRunnable1).start();
        }
    }
}
